package com.itheima.test;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.*;

/**
 * 导出一个有样式的Excel
 * 1. 边框线， 2.合并单元格， 3.行高列宽， 4.对齐方式， 5. 字体
 *
 * @author mewp
 * @date 2022/11/22 09:25
 **/
@SpringBootTest
public class POIDemo04 {
    @Test
    public void test01() throws Exception {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("有样式的数据");

        Cell cell = null;

        //大标题行
        Row bigTitleRow = sheet.createRow(0);
        //行高
        bigTitleRow.setHeightInPoints(42);
        CellStyle bigTitleRowCellStyle = workbook.createCellStyle();
        //边框：下，左，右，上
        bigTitleRowCellStyle.setBorderBottom(BorderStyle.THIN);
        bigTitleRowCellStyle.setBorderLeft(BorderStyle.THIN);
        bigTitleRowCellStyle.setBorderRight(BorderStyle.THIN);
        bigTitleRowCellStyle.setBorderTop(BorderStyle.THIN);
        //对齐方式：水平对齐、垂直对齐
        bigTitleRowCellStyle.setAlignment(HorizontalAlignment.CENTER);
        bigTitleRowCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        //字体：黑体，字号18
        Font bigFont = workbook.createFont();
        bigFont.setFontName("黑体");
        bigFont.setFontHeightInPoints((short) 18);
        bigTitleRowCellStyle.setFont(bigFont);
        for (int i = 0; i < 5; i++) {
            cell = bigTitleRow.createCell(i);
            cell.setCellStyle(bigTitleRowCellStyle);
        }
        //合并单元格：int firstRow起始行, int lastRow结束行, int firstCol起始列, int lastCol结束列
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 4));
        sheet.getRow(0).getCell(0).setCellValue("用户信息数据");

        //小标题行
        Row littleTitleRow = sheet.createRow(1);
        //行高
        littleTitleRow.setHeightInPoints(32);
        CellStyle littleTitleRowCellStyle = workbook.createCellStyle();
        //样式克隆
        littleTitleRowCellStyle.cloneStyleFrom(bigTitleRowCellStyle);
        //字体：黑体，字号12，加粗
        Font littleFont = workbook.createFont();
        littleFont.setFontName("宋体");
        littleFont.setFontHeightInPoints((short) 12);
        littleFont.setBold(true);
        littleTitleRowCellStyle.setFont(littleFont);
        String[] titles = {"编号", "姓名", "手机号", "入职日期", "现住址"};
        for (int i = 0; i < titles.length; i++) {
            cell = littleTitleRow.createCell(i);
            cell.setCellStyle(littleTitleRowCellStyle);
            cell.setCellValue(titles[i]);
        }

        //内容行
        Row contentTitleRow = null;
        CellStyle contentTitleRowCellStyle = workbook.createCellStyle();
        //样式克隆
        contentTitleRowCellStyle.cloneStyleFrom(bigTitleRowCellStyle);
        contentTitleRowCellStyle.setAlignment(HorizontalAlignment.LEFT);
        //字体：黑体，字号12
        Font contentFont = workbook.createFont();
        contentFont.setFontName("宋体");
        contentFont.setFontHeightInPoints((short) 11);
        contentFont.setBold(false);
        contentTitleRowCellStyle.setFont(contentFont);

        //把工作簿输出到本地磁盘
        workbook.write(new FileOutputStream("D:\\upload\\test\\style.xlsx"));
    }
}
